def split_hex_file(input_file, output_file, split_len):
    # 读取输入文件内容并去除空白字符
    with open(input_file, 'r') as f_in:
        hex_str = f_in.read().strip().replace('\n', '').replace(' ', '')
    
    # 验证字符串长度是否为split_len的倍数
    if len(hex_str) % split_len != 0:
        raise ValueError(f"十六进制字符串长度必须为{split_len}的倍数")
    
    # 写入每行split_len个字符的输出文件
    with open(output_file, 'w') as f_out:
        for i in range(0, len(hex_str), split_len):
            line = hex_str[i:i+split_len]
            f_out.write(line + '\n')

if __name__ == "__main__":
    input_file = "D:/FPGA/Full/TPU/SA/TPU/top/systolic_array/testbench/int4_int32/m32n8k16/c_int32_m32n8k16.txt"  # 输入文件名
    output_file = "c_int32_m32n8k16_hex.txt"  # 输出文件名
    split_len = 8  # 分割长度，可根据需要修改
    split_hex_file(input_file, output_file, split_len)
    print(f"文件已按每{split_len}字符分割保存至: {output_file}")